import request from '@/utils/request'
import { systemServUrl } from '@/utils/constants'
import axios from 'axios'

// 资源操作

export function getResourceListByRoleId(roleId) {
  return request({
    url: systemServUrl + '/role_resource/role/' + roleId,
    method: 'get'
  })
}

export function getResourceList(params) {
  return request({
    url: systemServUrl + '/resources',
    method: 'get',
    params
  })
}

// 角色操作

export function getRoleList(params) {
  return request({
    url: systemServUrl + '/roles/',
    method: 'get',
    params
  })
}

export function getRoleListByUserId(userId) {
  return request({
    url: systemServUrl + '/user_role/user/' + userId,
    method: 'get'
  })
}

export function insertRole(data) {
  return request({
    url: systemServUrl + '/role',
    method: 'post',
    data
  })
}

export function updateRoleById(data) {
  return request({
    url: systemServUrl + `/role/${data.id}`,
    method: 'put',
    data
  })
}

export function getRoleById(roleId) {
  return request({
    url: systemServUrl + `/role/${roleId}`,
    method: 'get'
  })
}

export function delRoleById(roleId) {
  return request({
    url: systemServUrl + `/role/${roleId}`,
    method: 'delete'
  })
}

// user-role 关联操作
export function insertUserRole(data) {
  return request({
    url: systemServUrl + '/user_role',
    method: 'post',
    data
  })
}

export function delUserRole(data) {
  return request({
    url: systemServUrl + '/user_role',
    method: 'delete',
    data
  })
}

// user-resource 关联操作
export function insertRoleResource(data) {
  return request({
    url: systemServUrl + '/role_resource',
    method: 'post',
    data
  })
}

export function delRoleResource(data) {
  return request({
    url: systemServUrl + '/role_resource',
    method: 'delete',
    data
  })
}

// 用户操作

export function getUserList(params) {
  return request({
    url: systemServUrl + '/users/',
    method: 'get',
    params
  })
}

export function getUserById(userId) {
  return request({
    url: systemServUrl + `/user/${userId}`,
    method: 'get'
  })
}

export function ifUsernameExist(username) {
  return request({
    url: systemServUrl + '/username',
    method: 'get',
    params: {
      name: username
    }
  })
}

export function insertUser(data) {
  return request({
    url: systemServUrl + '/user',
    method: 'post',
    data
  })
}

export function updateUser(data) {
  return request({
    url: systemServUrl + `/user/${data.id}`,
    method: 'put',
    data
  })
}

export function delUserById(userId) {
  return request({
    url: systemServUrl + `/user/${userId}`,
    method: 'delete'
  })
}

// 认证部分

export function getVerificationCode() {
  return axios({
    url: systemServUrl + '/captcha.jpg',
    method: 'get',
    responseType: 'arraybuffer'
  })
}

export function logout(params) {
  return request({
    url: systemServUrl + '/logout',
    method: 'get',
    params
  })
}

export function login(data) {
  return request({
    url: systemServUrl + '/login',
    method: 'post',
    data
  })
}

export function getUserInfoByToken(token) {
  return request({
    url: systemServUrl + '/auth',
    method: 'get',
    params: { token }
  })
}


export function getStatisticInfoApi() {
  return request({
    url: systemServUrl + "/statistic",
    method: 'get'
  })
}


export function getChartInfoApi() {
  return request({
    url: systemServUrl + "/chart",
    method: 'get'
  })
}


